﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Controls.Primitives;

namespace SharpSoft.WPF.Controls
{

    public class LeftMenuItemCollection : ObservableCollection<LeftMenuItem>
    {
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnCollectionChanged(e);
            if (e.NewItems != null)
            {
                foreach (var item in e.NewItems)
                {
                    if (item is LeftMenuItem leftMenuItem)
                    {
                        leftMenuItem.Parent = this;
                    }
                }
            }
        }
        /// <summary>
        /// 添加一个子项并返回当前集合
        /// </summary>
        /// <param name="title"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public LeftMenuItemCollection AddItem(string title, object tag = null)
        {
            var item = new LeftMenuItem(title, tag);
            this.Add(item);
            return this;
        }

        public LeftMenuItemCollection AddItemContainer(string title, object tag = null)
        {
            var item = new LeftMenuItem(title, tag);
            this.Add(item);
            return item.Items;
        }
        /// <summary>
        /// 返回上一级
        /// </summary>
        /// <returns></returns>
        public LeftMenuItemCollection Up()
        {
            if (this.Parent is LeftMenu lm)
            {
                return lm.Items;
            }
            else if (this.Parent is LeftMenuItem lmi)
            {
                return lmi.Parent;
            }
            else
            {
                throw new Exception("无效的父容器。");
            }
        }

        public DependencyObject Parent { get; protected internal set; }
    }

    public class LeftMenuItem : ButtonBase
    {
        public LeftMenuItem()
        {
            Items = new LeftMenuItemCollection() { Parent = this };
            Items.CollectionChanged += Items_CollectionChanged;
        }

        public LeftMenuItem(string title, object tag = null) : this()
        {
            Title = title;
            Tag = tag;
        }
        public new LeftMenuItemCollection Parent { get; protected internal set; }

        private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            HasItems = Items.Count > 0;
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(LeftMenuItem), new PropertyMetadata(""));

         

        public bool HasItems
        {
            get { return (bool)GetValue(HasItemsProperty); }
            private set { SetValue(HasItemsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HasItems.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HasItemsProperty =
            DependencyProperty.Register("HasItems", typeof(bool), typeof(LeftMenuItem), new PropertyMetadata(false));

         

        [Category("图标")]
        public ImageSource IconSource
        {
            get { return (ImageSource)GetValue(IconSourceProperty); }
            set { SetValue(IconSourceProperty, value); }
        }

        public static readonly DependencyProperty IconSourceProperty =
            DependencyProperty.Register("IconSource", typeof(ImageSource), typeof(LeftMenuItem), new PropertyMetadata(null));



        [Category("图标")]
        [Description("图标的停靠方式")]
        public Dock IconDock
        {
            get { return (Dock)GetValue(IconDockProperty); }
            set { SetValue(IconDockProperty, value); }
        }

        public static readonly DependencyProperty IconDockProperty =
            DependencyProperty.Register("IconDock", typeof(Dock), typeof(LeftMenuItem), new PropertyMetadata(Dock.Left));



        [Category("图标")]
        [Description("图标的外边距")]
        public Thickness IconMargin
        {
            get { return (Thickness)GetValue(IconMarginProperty); }
            set { SetValue(IconMarginProperty, value); }
        }

        public static readonly DependencyProperty IconMarginProperty =
            DependencyProperty.Register("IconMargin", typeof(Thickness), typeof(LeftMenuItem), new PropertyMetadata(new Thickness(3)));



        public LeftMenuItemCollection Items
        {
            get { return (LeftMenuItemCollection)GetValue(ItemsProperty); }
              set { SetValue(ItemsProperty, value); }
        }

        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(LeftMenuItemCollection), typeof(LeftMenuItem), new PropertyMetadata(null));




    }
}
